package com.cykj.faceledtv;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.multidex.MultiDex;

import com.cykj.faceledtv.data.DataManager;
import com.cykj.faceledtv.di.component.ApplicationComponent;
import com.cykj.faceledtv.di.component.DaggerApplicationComponent;
import com.cykj.faceledtv.di.module.ApplicationModule;
import com.cykj.faceledtv.ui.splash.SplashActivity;
import com.nanchen.crashmanager.CrashApplication;
import com.nanchen.crashmanager.UncaughtExceptionHandlerImpl;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;

import java.util.HashSet;
import java.util.Set;

import javax.inject.Inject;

import interfaces.heweather.com.interfacesmodule.view.HeConfig;

/**
 * TODO 横竖屏旋转 横屏高度显示超限
 * TODO 掉线无法重连
 *
 * @author jzh
 * @date 2018/12/11 17:32
 */

public class MyApp extends CrashApplication {
    public static MyApp instance;
    private Set<Activity> allActivities;
    /**
     * 数据管理器
     */
    @Inject
    public DataManager mDataManager;

    private ApplicationComponent mApplicationComponent;


    /**
     * 解决方法数超出限制，在5.0以下分包操作
     *
     * @param base 上下文
     */
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        //实例化dagger
        mApplicationComponent = DaggerApplicationComponent.builder()
                .applicationModule(new ApplicationModule(this)).build();
        mApplicationComponent.inject(this);
        //初始化和风天气
        HeConfig.init(BuildConfig.HE_ID, BuildConfig.HE_KEY);
        HeConfig.switchToFreeServerNode();
        // 设置崩溃后自动重启 APP
// 参数依次为 上下文（建议是Application），是否是debug模式，是否崩溃后重启，重启延迟时间，重启的Activity
        UncaughtExceptionHandlerImpl.getInstance().init(this, BuildConfig.DEBUG, true, 0, SplashActivity.class);
        //初始化logger
        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder().tag("TAG").build();
        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy) {
            @Override
            public boolean isLoggable(int priority, @Nullable String tag) {
                return BuildConfig.DEBUG;
            }
        });
    }

    public static MyApp getInstance() {
        if (instance == null) {
            synchronized (MyApp.class) {
                if (instance == null) {
                    instance = new MyApp();
                }
            }
        }
        return instance;
    }

    public ApplicationComponent getComponent() {
        return mApplicationComponent;
    }

    /**
     * 添加activity
     */
    public void addActivity(Activity act) {
        if (allActivities == null) {
            allActivities = new HashSet<>();
        }
        allActivities.add(act);
    }

    /**
     * 移除activity
     */
    public void removeActivity(Activity act) {
        if (allActivities != null) {
            allActivities.remove(act);
        }
    }

    /**
     * 获取activity栈
     *
     * @return setF
     */
    public Set<Activity> getAllActivities() {
        if (allActivities != null) {
            return allActivities;
        }
        return null;
    }

    /**
     * 退出
     */
    public void exit() {
        for (Activity activity : allActivities) {
            if (activity != null) {
                activity.finish();
            }
        }

    }
}
